package com.qu.waibao.baidu.openim;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.aop.Pointcut;
import com.alibaba.mobileim.aop.custom.IMChattingPageUI;
import com.alibaba.mobileim.channel.util.AccountUtils;
import com.alibaba.mobileim.contact.IYWContact;
import com.alibaba.mobileim.conversation.YWConversation;
import com.alibaba.mobileim.conversation.YWConversationType;
import com.alibaba.mobileim.conversation.YWP2PConversationBody;
import com.alibaba.mobileim.conversation.YWTribeConversationBody;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;

/**
 * @描述 describe
 * @作者 Administrator
 * @创建时间 2018/8/8 11:11
 * @邮箱 1123599561@qq.com
 */
public class ChattingUICustomSample extends IMChattingPageUI {
    public ChattingUICustomSample(Pointcut pointcut) {
        super(pointcut);
    }



    @Override
    public boolean needHideChattingReplyBar(YWConversation conversation) {
        Log.i("abc",conversation.getConversationId()+"****");
        return false;
    }
    @Override
    public View getCustomTitleView(final Fragment fragment,
                                   final Context context, LayoutInflater inflater,
                                   final YWConversation conversation) {
        //TODO 重要：必须以该形式初始化view---［inflate(R.layout.**, new RelativeLayout(context),false)］------，以让inflater知道父布局的类型，否则布局**中的高度和宽度无效，均变为wrap_content
        View view = inflater.inflate(R.layout.kefu_top_layout, new LinearLayout(context), false);
        TextView textView = (TextView) view.findViewById(R.id.tv_title);
        String title = null;
        if (conversation.getConversationType() == YWConversationType.P2P) {
            YWP2PConversationBody conversationBody = (YWP2PConversationBody) conversation
                    .getConversationBody();
            if (!TextUtils.isEmpty(conversationBody.getContact().getShowName())) {
                title = conversationBody.getContact().getShowName();
            } else {
                YWIMKit imKit = YWAPI.getIMKitInstance(AppConfig.getString("userid"), AppConfig.APP_KEY);
                IYWContact contact = imKit.getContactService().getContactProfileInfo(conversationBody.getContact().getUserId(), conversationBody.getContact().getAppKey());
                //生成showName，According to id。
                if (contact != null && !TextUtils.isEmpty(contact.getShowName())) {
                    title = contact.getShowName();
                }
            }
            //如果标题为空，那么直接使用Id
            if (TextUtils.isEmpty(title)) {
                title = conversationBody.getContact().getUserId();
            }
        } else {
            if (conversation.getConversationBody() instanceof YWTribeConversationBody) {
                title = ((YWTribeConversationBody) conversation.getConversationBody()).getTribe().getTribeName();
                if (TextUtils.isEmpty(title)) {
                    title = "团队群聊";
                }
            } else {
                if (conversation.getConversationType() == YWConversationType.SHOP) { //为OpenIM的官方客服特殊定义了下、
                    title = AccountUtils.getShortUserID(conversation.getConversationId());
                }
            }
        }

        if (conversation.getConversationId().equals("i1mnr46ikefu")){
            textView.setText("客服");
        }else {
            textView.setText(title);
        }
        LinearLayout backView = (LinearLayout) view.findViewById(R.id.ll_back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                fragment.getActivity().finish();
            }
        });
        if(title.equals("在线客服")){
            backView.setVisibility(View.GONE);
        }else {
            backView.setVisibility(View.VISIBLE);
        }
        return view;
    }
}